package org.luxor.commons.storage.client.fastdfs;

import com.luhuiguo.fastdfs.conn.ConnectionManager;
import com.luhuiguo.fastdfs.conn.FdfsConnectionPool;
import com.luhuiguo.fastdfs.conn.TrackerConnectionManager;
import com.luhuiguo.fastdfs.domain.StorePath;
import com.luhuiguo.fastdfs.service.AppendFileStorageClient;
import com.luhuiguo.fastdfs.service.DefaultAppendFileStorageClient;
import com.luhuiguo.fastdfs.service.DefaultTrackerClient;
import org.luxor.commons.storage.client.AbstractStorageClient;
import org.luxor.commons.storage.client.AbstractStorageProperties;
import org.luxor.commons.storage.exception.StorageTypeNotSupportedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * FastDfs 文件存储服务
 *
 * @author Mr.yan @date 2019/12/15
 */
public class FastDfsStorageClient extends AbstractStorageClient {
    private static final Logger logger = LoggerFactory.getLogger(FastDfsStorageClient.class);
    /**
     * FastDfs 全局配置项
     */
    private final FastDfsProperties properties;

    private AppendFileStorageClient storageClient;

    public FastDfsStorageClient(FastDfsProperties properties) {
        this.properties = properties;
        if (this.isEnabled()) {
            this.init();
        }
    }

    @Override
    public AbstractStorageProperties getConfig() {
        return this.properties;
    }

    @Override
    public boolean isEnabled() {
        return this.properties.isEnabled();
    }

    private void init() {
        logger.info("Initialization FastDfs open ( Running FastDfs Connector)");
        try {
            FdfsConnectionPool pool = properties.getPool();
            List<String> trackers = properties.getTrackerList();

            TrackerConnectionManager tcm = new TrackerConnectionManager(pool, trackers);
            DefaultTrackerClient trackerClient = new DefaultTrackerClient(tcm);

            ConnectionManager acm = new ConnectionManager(pool);
            this.storageClient = new DefaultAppendFileStorageClient(trackerClient, acm);

            logger.info("Initialize FastDfs Connector Success,bye");
        } catch (Throwable e) {
            logger.error("Initialize FastDfs Connector Failure!", e);
            throw e;
        }
    }

    @Override
    public String upload(byte[] fileByte, String fileName, String suffix) {
        if (!this.isEnabled()) {
            logger.error("[未开启] FastDfs分布式存储功能");
            throw new StorageTypeNotSupportedException("[未开启] FastDfs分布式存储功能");
        }
        StorePath storePath = storageClient.uploadFile(properties.getGroupName(), fileByte, suffix);
        String fileFullPath = storePath.getFullPath();

        logger.info("上传文件,路径:{}", fileFullPath);
        return fileFullPath;
    }

    @Override
    public byte[] download(String fileFullPath) {
        if (!this.isEnabled()) {
            logger.error("[未开启] FastDfs分布式存储功能. fileFullPath:{}", fileFullPath);
            throw new StorageTypeNotSupportedException("[未开启] FastDfs分布式存储功能");
        }
        logger.info("下载文件,路径:{}", fileFullPath);

        StorePath storePath = StorePath.praseFromUrl(fileFullPath);
        return storageClient.downloadFile(storePath.getGroup(), storePath.getPath());
    }

    @Override
    public void delete(String fileFullPath) {
        if (!this.isEnabled()) {
            logger.error("[未开启] FastDfs分布式存储功能. fileFullPath:{}", fileFullPath);
            throw new StorageTypeNotSupportedException("[未开启] FastDfs分布式存储功能");
        }
        logger.info("删除文件,路径:{}", fileFullPath);

        StorePath storePath = StorePath.praseFromUrl(fileFullPath);
        storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
    }

}
